package com.zhao.utils;

import com.zhao.common.CommonData;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Description: 连接池DBCP操作工具类<br/>
 *
 * @author 柒柒
 * @version : 1.0
 * @ClassName DBCPUtil <br/>
 * Copyright (c) ，2020 ， 赵 <br/>
 * Date： 2020/12/10 15:29
 */

public class DBCPUtil {
    //连接池
    private static DataSource pool;

    //初始化连接池
    static {
        Properties properties = new Properties();

        try {
            properties.load(DBCPUtil.class.getClassLoader().getResourceAsStream(CommonData.DB_CONN_PROPERTIES));
            pool = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回连接池的实例
     * @return
     */
    public static DataSource getConnectionPool(){
        return pool;
    }

    /**
     * 获得连接池实例
     * @return
     */
    public static Connection getConnection(){
        try {
            return pool.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(CommonData.DB_CONN_EXCEPTION_INFO);
        }
    }

}




















